<template>
	<view class="copy-container">
		<view class="form-content">
			<up-form labelPosition="left" :model="copyForm" ref="copyFormRef" label-width="140">
				<up-form-item label="要复制编号" prop="code" borderBottom @click="openPicker">
					<up-input v-model="copyForm.code" disabled disabled-color="#ffffff" placeholder="请输入需要复制的编号"
						border="none"></up-input>
					<template #right>
						<up-icon name="arrow-down"></up-icon>
					</template>
				</up-form-item>
				<up-form-item label="复制后编号" prop="newCode" borderBottom>
					<up-input v-model="copyForm.newCode" placeholder="请输入新编号" border="none"></up-input>
				</up-form-item>
			</up-form>
			<up-picker :show="pickerShow" ref="uPickerRef" :columns="[pickerData]" :keyName="keyName"
				@confirm="onPickerConfirm" @cancel="pickerShow=false"></up-picker>

			<view class="bottom-btn flex justify-around">
				<up-button type="primary" color="#246eff" shape="circle" customStyle="width:40%"
					@click="handleSubmit">复制</up-button>
				<up-button shape="circle" color="#999" customStyle="width:40%;margin-left:20rpx;"
					@click="handleCancel">取消</up-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		quergyBoiler,
		quergyNewEnergy,
		queryHeatMeter,
		copyData
	} from '../../api/company';
	export default {
		data() {
			return {
				dialogType: 'boiler',
				keyName: 'code',
				energyInfo: {},
				copyForm: {
					newCode: '',
					code: '',
					id: '',
				},
				pickerShow: false,
				pickerData: [],

			}
		},
		mounted() {

		},
		onLoad(options) {

			this.initData(options)
		},
		methods: {
			initData(options) {
				const {
					type,
					info
				} = options;
				this.dialogType = type;
				// this.keyName=type=='boiler'?'code':type == 'newEnergy'?'name':'materCode';
				this.energyInfo = JSON.parse(decodeURIComponent(info));
				uni.setNavigationBarTitle({
					title: type == 'boiler' ? '锅炉复制' : type == 'newEnergy' ? '新能源复制' : '热表复制'
				});
			},
			async openPicker() {
				const {
					stationId
				} = this.energyInfo;
				if (this.dialogType == 'boiler') {
					this.keyName = 'code';
					const res = await quergyBoiler(stationId, {
						checkTag: 1
					});
					const list = res || [];
					this.pickerData = list;
				} else if (this.dialogType == 'newEnergy') {
					this.keyName = 'name';
					const data = await quergyNewEnergy(stationId, {
						checkTag: 1
					});
					this.pickerData = data || [];
				} else {
					this.keyName = 'materCode';
					const list = await queryHeatMeter(stationId, {
						checkTag: 1
					});
					this.pickerData = list || [];
				}
				this.pickerShow = true;
			},
			onPickerConfirm(e) {
				this.copyForm.code = e.value[0][this.keyName];
				this.copyForm.id = e.value[0].id;
				this.pickerShow = false;

			},
			handleSubmit() {

				const {
					id,
					newCode
				} = this.copyForm;
				if(!id){
					return uni.$u.toast('要复制的编号不能为空')
				}
				if(!newCode){
					return uni.$u.toast('新编号不能为空')
				}
				const params = {
					id:id
				};
				if (this.dialogType == 'boiler') {
					params.newCode = newCode
				} else if (this.dialogType == 'newEnergy') {
					params.newName = newCode
				} else {
					params.newMaterCode = newCode
				}
				
				copyData(this.dialogType,params).then((res) => {
					uni.$u.toast('复制成功');
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index',
						});
					}, 1000);
				});
			},
			/**
			 * 取消
			 */
			handleCancel() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.copy-container {
		width: 100%;
		min-height: 100vh;
		padding: 30rpx 20rpx;
		box-sizing: border-box;
		position: relative;
		background: #f8f8f8;
	}

	.form-content {
		padding: 20rpx 20rpx 40rpx 20rpx;
		width: 100%;
		box-sizing: border-box;
		background: #fff;
		border-radius: 12rpx;
	}

	.bottom-btn {
		margin-top: 60rpx;
	}
</style>